/**
 * 
 */
package com.letv.so.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xingzhiyuan
 *
 */
public class ReflectUtil {
    private static final Logger logger = LoggerFactory.getLogger(ReflectUtil.class);
	public static final String GET_STR = "get";
	public static final String SET_STR = "set";
	
	/**
	 * 
	 * @param owner
	 * @param fieldname
	 * @param optStr
	 * @return
	 */
    public static Object invokeGetMethod(Object owner, String fieldname){
    	Object rtn = null;
        Class<? extends Object> ownerClass = owner.getClass();
        
        Method method = null;
        try {
			method = ownerClass.getMethod(toGetOrSet(fieldname,GET_STR));
		} catch (SecurityException e) {
			logger.error("get "+fieldname+" Error!",e);
			return rtn;
		} catch (NoSuchMethodException e) {
			logger.error("get "+fieldname+" Error!",e);
			return rtn;
		}
        try {
        	rtn = method.invoke(owner);
		} catch (IllegalArgumentException e) {
			logger.error("get "+fieldname+" Error!",e);
			return rtn;
		} catch (IllegalAccessException e) {
			logger.error("get "+fieldname+" Error!",e);
			return rtn;
		} catch (InvocationTargetException e) {
			logger.error("get "+fieldname+" Error!",e);
			return rtn;
		}
        return rtn;
    }


    /**
     * 
     * @param <T>
     * @param owner
     * @param fieldname
     * @return boolean 设置成功返回true
     */
    public static <T> boolean invokeSetMethod(Object owner, String fieldname,Object value,Class<T> parameterType){
    	boolean rtn = false;
//    	ClassLoader loader = Thread.currentThread().getContextClassLoader(); 
        Class<? extends Object> ownerClass = owner.getClass();
        if (value == null)
        	return rtn;
        Method method = null;
        try {
        	String methodName = toGetOrSet(fieldname,SET_STR);
			method = ownerClass.getMethod(methodName,parameterType);
		} catch (SecurityException e) {
			logger.error("set "+fieldname+" Error!",e);
			return rtn;
		} catch (NoSuchMethodException e) {
			logger.error("set "+fieldname+" Error!",e);
			return rtn;
		}
        try {
        	method.invoke(owner,value);
        	rtn = true;
		} catch (IllegalArgumentException e) {
			logger.error("set "+fieldname+" Error!",e);
			return rtn;
		} catch (IllegalAccessException e) {
			logger.error("set "+fieldname+" Error!",e);
			return rtn;
		} catch (InvocationTargetException e) {
			logger.error("set "+fieldname+" Error!",e);
			return rtn;
		}
        return rtn;
    }

    /**
     * Get/Set  method name by field name
     * @param fieldname
     * @return
     */
    private static String toGetOrSet(String fieldname,String optStr) {
        
        if (fieldname == null || fieldname.length() == 0) {
            return null;
        }
        
        /* If the second char is upper, make 'get' + field name as getter name. For example, eBlog -> geteBlog */
        if (fieldname.length() > 2) {
            String second = fieldname.substring(1, 2);
            if (second.equals(second.toUpperCase())) {
                return new StringBuffer(optStr).append(fieldname).toString();
            }
        }
        
        /* Common situation */
        fieldname = new StringBuffer(optStr).append(fieldname.substring(0, 1).toUpperCase())
                .append(fieldname.substring(1)).toString();
        
        return  fieldname;
    }
    
    public static void main(String...strings) {
    }

}
